package com.goods.business.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.productConverter;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper Mapper;

    /**
     * 获取所有类别
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(int pageNum, int pageSize) {
        //查询对象，
        List<ProductCategory> productCategories = Mapper.selectAll();

        ArrayList<ProductCategoryTreeNodeVO> productCategoryVOS = new ArrayList<>();
        //判断
        if (productCategories != null && productCategories.size() > 0){
            for (ProductCategory productCategory : productCategories) {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO = productConverter.ToProductCategoryTreeNodeVO(productCategory);
                productCategoryVOS.add(productCategoryTreeNodeVO);
            }
        }
        //然后对其进行子封装
        List<ProductCategoryTreeNodeVO> result = CategoryTreeBuilder.build(productCategoryVOS);


        //封装到PageInfo对象
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<ProductCategoryTreeNodeVO> pageInfo = new PageInfo<>(result);
        //返回数据
        return new PageVO(pageInfo.getTotal(),pageInfo.getList());
    }


    /**
     * 获取添加时的分类数据
     * @return
     */
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        //查询对象，
        List<ProductCategory> productCategories = Mapper.selectAll();

        ArrayList<ProductCategoryTreeNodeVO> productCategoryVOS = new ArrayList<>();
        //判断
        if (productCategories != null && productCategories.size() > 0){
            for (ProductCategory productCategory : productCategories) {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO = productConverter.ToProductCategoryTreeNodeVO(productCategory);
                productCategoryVOS.add(productCategoryTreeNodeVO);
            }
        }
        //然后对其进行子封装
        List<ProductCategoryTreeNodeVO> result = CategoryTreeBuilder.build(productCategoryVOS);
        return result;
    }


    /**
     * 添加分类数据
     */
    @Override
    public void add(ProductCategory productCategory) {
        //赋值
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        //添加
        Mapper.insert(productCategory);
    }

    /**
     * 回显分类
     * @param id
     */
    @Override
    public ProductCategoryVO edit(Long id) {
        //查询分类
        ProductCategory productCategory = Mapper.selectByPrimaryKey(id);
        //判断
        if (productCategory != null){
            return productConverter.ProductCategoryToVO(productCategory);
        }
        return null;
    }

    /**
     * 保存编辑分类
     * @param productCategory
     */
    @Override
    public void update(ProductCategory productCategory) {
       Mapper.updateByPrimaryKey(productCategory);
    }



    /**
     * 删除分类
     * @param id
     */
    @Override
    public void delete(Long id) {
        Mapper.deleteByPrimaryKey(id);
    }
}
